package com.example.security.controller;

import com.example.security.annotation.HasPermission;
import com.example.security.dto.RoleDTO;
import com.example.security.entity.SysRole;
import com.example.security.service.RoleService;
import com.example.security.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/api/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @GetMapping("/list")
    @Operation(summary = "获取角色列表")
    public Result<List<SysRole>> list() {
        return Result.ok(roleService.getAllRoles());
    }

    @PostMapping
    @Operation(summary = "创建角色")
    public Result<SysRole> add(@RequestBody @Valid RoleDTO roleDTO) {
        return Result.ok(roleService.createRole(roleDTO));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新角色")
    public Result<SysRole> update(
            @PathVariable Long id,
            @RequestBody @Valid RoleDTO roleDTO
    ) {
        return Result.ok(roleService.updateRole(id, roleDTO));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色")
    public Result<Void> delete(@PathVariable Long id) {
        roleService.deleteRole(id);
        return Result.ok();
    }

    @PutMapping("/{roleId}/permissions")
    @Operation(summary = "分配角色权限")
    public Result<Void> assignPermissions(
            @PathVariable Long roleId,
            @RequestBody List<Long> permissionIds
    ) {
        roleService.assignPermissions(roleId, permissionIds);
        return Result.ok();
    }
} 